<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2019 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Pay.php 41522 2019-01-30 15:02:22 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：入账记录 对应的表名:tky_pay
 * +----------------------------------------------------------------------
 */

namespace application\member\controller;

use application\admin\controller\base as auth;
use ticky\request;
use ticky\response;

class pay extends auth {

    // 首页
    public function index() {
        $search = $this->search_frm();
        $ret = db('pay')->where($search['sql'])->order('id desc')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->assign('search', $search['arr']);
        $this->display('pay/index');
    }

    // 添加记录
    public function add() {
        if (request::isPost()) {
            $data = $this->post_frm();
            db('pay')->insert($data);
            showmsg('添加成功', '/member/pay');
        } else {
            $this->assign('postUrl', '/member/pay/add');
            $this->assign('action', '添加');
            $this->display('pay/manage');
        }
    }

    // 更新记录
    public function update() {
        if (request::isPost()) {
            $data = $this->post_frm();
            $id = request::post('id', 0);
            db('pay')->where(['id' => $id])->update($data);
            showmsg('修改成功', '/member/pay');
        } else {
            $id = request::get('id', 0);
            $item = db('pay')->where(['id' => $id])->find();
            $this->assign('postUrl', '/member/pay/update');
            $this->assign('action', '修改');
            $this->assign('item', $item);
            $this->display('pay/manage');
        }
    }

    // 删除记录
    public function delete() {
        $id = request::get('id', 0);
        $result = db('pay')->delete(['id' => $id]);
        showmsg('删除成功', '/member/pay');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', 'del');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db('pay')->delete(['id' => $id]);
            }
            showmsg('删除成功', '/member/pay');
        }
    }

    //查询条件
    private function search_frm() {
        $search = request::get('search', []);
        $where = '1=1 ';

        $type = request::get('type', '');
        $keyword = request::get('keyword', '');
        $search['type'] = $type;
        if ($keyword != '') {
            $search['keyword'] = $keyword;
            switch ($type) {
                case "id":
                    $where .= "and id = '" . $keyword . "' ";
                    break;
                case "trade_sn":
                    $where .= "and trade_sn = '" . $keyword . "' ";
                    break;
                case "userid":
                    $where .= "and userid = '" . $keyword . "' ";
                    break;
                case "username":
                    $where .= "and username = '" . $keyword . "' ";
                    break;
                case "money":
                    $where .= "and money = '" . $keyword . "' ";
                    break;
                case "creat_time":
                    $where .= "and creat_time = '" . $keyword . "' ";
                    break;
                case "msg":
                    $where .= "and msg = '" . $keyword . "' ";
                    break;
                case "type":
                    $where .= "and type = '" . $keyword . "' ";
                    break;
                case "ip":
                    $where .= "and ip = '" . $keyword . "' ";
                    break;
                case "status":
                    $where .= "and status = '" . $keyword . "' ";
                    break;
                case "remarks":
                    $where .= "and remarks = '" . $keyword . "' ";
                    break;
                case "adminnote":
                    $where .= "and adminnote = '" . $keyword . "' ";
                    break;
            }
        }
        return [
            'arr' => $search,
            'sql' => $where
        ];
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['trade_sn'] = request::post('trade_sn', '');
        $arr['userid'] = request::post('userid', '');
        $arr['username'] = request::post('username', '');
        $arr['money'] = request::post('money', '');
        $arr['creat_time'] = request::post('creat_time', '');
        $arr['msg'] = request::post('msg', '');
        $arr['type'] = request::post('type', '');
        $arr['ip'] = request::post('ip', '');
        $arr['status'] = request::post('status', '');
        $arr['remarks'] = request::post('remarks', '');
        $arr['adminnote'] = request::post('adminnote', '');
        return $arr;
    }

}
